//
//  ZLBurnAfterReadingAttachmentDecoder.m
//  ZLYoYoIM_Example
//
//  Created by 赵磊 on 2021/1/20.
//  Copyright © 2021 itzhaolei. All rights reserved.
//

#import "ZLAttachmentDecoder.h"
#import "ZLAttachment.h"
#import "ZLSessionCustomTypeHeader.h"

@implementation ZLAttachmentDecoder

- (id<NIMCustomAttachment>)decodeAttachment:(NSString *)content{
    id<NIMCustomAttachment> attachment;
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    if (data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        if ([dict isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dataDict = dict[@"data"];
            NSString *type = dict[@"type"];
            NSString *urlKey = [type intValue] != ZLSessionCustomTypeBurnAfterReading ? @"urlkey" : @"url";
            NSString *url = dataDict[urlKey];
            ZLAttachment *myAttachment = [[ZLAttachment alloc] init];
            myAttachment.url = url;
            myAttachment.type = type;
            attachment = myAttachment;
        }
    }
    return attachment;
}

@end
